/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.mleiria.app.web.graphics;

import java.util.Map;
import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.BarChartModel;
import org.primefaces.model.chart.ChartSeries;

/**
 *
 * @author manuel
 */
public class BarChart {
    private final String title;
    private final String label;
    private final String xLabel;
    private final String yLabel;
    private double maxY;
    private BarChartModel model;
    private Map<Object, Number> barData;

    public BarChart(final String title, final String xLabel, final String yLabel, final String label) {
        this.title = title;
        this.xLabel = xLabel;
        this.yLabel = yLabel;
        this.label = label;
    }
    
    
    public void setMaxY(final double maxY){
        this.maxY = maxY;
    }

    public void setBarData(Map<Object, Number> barData) {
        this.barData = barData;
    }
    
    
    
    public BarChartModel createBarChartModel(final Map<Object, Number> barData){
        initBarModel(barData);
        createBarModel();
        return model;
    }
    
    private void initBarModel(final Map<Object, Number> barData) {
        model = new BarChartModel();

        ChartSeries englishFreqSeries = new ChartSeries();
        englishFreqSeries.setLabel(label);
        englishFreqSeries.setData(barData);
        model.addSeries(englishFreqSeries);
    }
    
    private void createBarModel(){
        model.setTitle(title);
        model.setLegendPosition("ne");

        Axis xAxis = model.getAxis(AxisType.X);
        xAxis.setLabel(xLabel);

        Axis yAxis = model.getAxis(AxisType.Y);
        yAxis.setLabel(yLabel);
        yAxis.setMin(0);
        yAxis.setMax(maxY);
    }
    
    
}
